<template>
  <view v-if="goodsInfo.goods_name">
    <!-- 轮播图 -->
    <swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000" circular>
      <swiper-item v-for="(item,index) in goodsInfo.pics" :key="index" class="goodsPic">
       <image :src="item.pics_big" @click="preview(index)"></image>
      </swiper-item>
    </swiper>
    
    <!-- 商品信息区域 -->
    <view class="goodsInfo-box">
      <view class="goodsPrice">￥{{goodsInfo.goods_price}}</view>
      <view class="goodsInfo-body">
        <view class="goods-name">{{goodsInfo.goods_name}}</view>
        <view class="favi">
          <uni-icons type="star" size="17"></uni-icons>
          <text>收藏</text>
        </view>
      </view>
      <view class="yunFei">快递：免运费</view>
    </view>
    
    <!-- 商品详细信息 -->
    <rich-text :nodes="goodsInfo.goods_introduce"></rich-text>
    
    
    <!-- 底部按钮 -->
    <view class="goods-nav">
        <uni-goods-nav :fill="true"  :options="options" :buttonGroup="buttonGroup"  @click="onClick" @buttonClick="buttonClick" />
    </view>
  </view>
</template>

<script>
  import {mapState,mapMutations,mapGetters} from'vuex'
  
  export default {
    computed:{
      ...mapState('m_cart',['cart']),
      ...mapGetters('m_cart',['total'])
    },
    watch:{
      // total(newValue){
      //   console.log(newValue)
      //   const findResult = this.options.find(item=>item.text==='购物车')
      //   if(findResult){
      //       findResult.info =newValue
      total:{
        handler(newValue){
          const findResult = this.options.find(item=>item.text==='购物车')
          if(findResult){
            findResult.info =newValue
        }
      },
      immediate:true
        }
      
      
    },
    data() {
      return {
        goodsInfo:{},
        options: 
        [
          {
        			icon: 'shop',
        			text: '店铺',
        			info: 0,
        			infoBackgroundColor:'#ff0000',
        			infoColor:"#fff"
        	}, 
          {
        			icon: 'cart',
        			text: '购物车',
        			info: 0
        		},
        ],
        buttonGroup: 
        [{
        	      text: '加入购物车',
        	      backgroundColor: '#ff0000',
        	      color: '#fff'
        	    },
        	    {
        	      text: '立即购买',
        	      backgroundColor: '#ffa200',
        	      color: '#fff'
        	    }
        	    ]
      };
    },
    onLoad(options) {
      this.getGoodsInfo(options.goods_id)
    },
    methods:{
      ...mapMutations('m_cart',['addtoCart']),
     async getGoodsInfo(goods_id){
        const res = await uni.$http.get('/api/public/v1/goods/detail',{goods_id})
        if(res.data.meta.status!==200) return uni.$showMsg
        res.data.message.goods_introduce = res.data.message.goods_introduce.replace(/<img /g, '<img style="display:block;" ').replace(/webp/g, 'jpg')
        this.goodsInfo = res.data.message
      },
      preview(index){
        uni.previewImage({
          current:index,
          urls:this.goodsInfo.pics.map(item=>item.pics_big)
        })
      },
      onClick(e){
        console.log(e)
        if(e.content.text==='购物车'){
          uni.switchTab({
            url:'/pages/cart/cart'
          })
        }
      },
      buttonClick(e){
        console.log(this.goodsInfo)
        if(e.content.text === '加入购物车'){
          // { goods_id, goods_name, goods_price, goods_count,goods_small_logo, goods_state }
          const goods = {
            goods_id :this.goodsInfo.goods_id,
            goods_name :this.goodsInfo.goods_name,
            goods_price : this.goodsInfo.goods_price,
            goods_count : 1,
            goods_small_logo : this.goodsInfo.goods_small_logo,
            goods_state : true//商品的勾选状态
          }
          this.addtoCart(goods)
        }
        
      }
    }
  }
</script>

<style lang="scss">
swiper{
  height: 750rpx;
  image{
    width: 100%;
    height: 100%;
  }
}
.goodsInfo-box{
  padding: 10px;
  padding-right: 0;
  .goodsPrice{
    color: #c00000;
    font-size: 18px;
    margin: 10px 0;
  }
  .goodsInfo-body{
    display: flex;
    justify-content: space-between;
    .goods-name{
      font-size: 13px;
      padding-right: 10px;
    }
    .favi{
      width: 120px;
      font-size: 12px;
      display: flex;
      flex-direction: column;
      justify-content: center;
      align-items: center;
      border-left: 1px solid #efefef;
      color: gray;
    }
  }
  .yunFei{
    margin: 10px 0;
    font-size: 12px;
    color: gray;
  }
}

.goods-nav{
  position: sticky;
  bottom: 0;
}

</style>
